<?php
/**
 * Created by PhpStorm.
 * User: adminchyistrator
 * Date: 2018/12/10
 * Time: 9:29
 */

namespace app\adminchy\controller;

use Think\Db;
use think\facade\Request;
use app\adminchy\controller\Base;
use app\yangpay\AlipayTransfer;

class Money extends Base
{
    //充值记录
    public function recharge(){
        $search = Request::param('search');
        $sql = db('wallet_log') ->field('id,status,user_id,amount,create_time')-> where(['type'=>2,'status'=>1]);
        if ($search){
            $sql->where('id',$search);
        }
        $rechargeInfo =$sql->paginate(15)->each(function ($item,$key){
            $item['status'] = $item['status'] == 0 ? '失败' :'成功';
            $item['user_name'] = db('user')->where(['user_id'=>$item['user_id']])->value('user_name');
            $item['user_phone'] = db('user')->where(['user_id'=>$item['user_id']])->value('user_phone');
            return $item;
        });
        $this->assign('rechargeInfo',$rechargeInfo);
        $this->assign('search',$search);
        return view('Money/recharge');
    }
    //提现记录
    public function withdraw(){
        $withdrawInfo = Db::name('withdraw_order w')->where('w.type','<>',0)->order('w.create_time','desc')->join('user u','u.user_id=w.user_id')->field('u.user_name,u.user_phone,w.*')->paginate(15)->each(function($item,$key)
            {
                $sArr = ['','已通过','已拒绝','已到账','到账失败'];
                $item['status'] = $sArr[$item['status']];
                return $item;
            });
        $this->assign('withdrawInfo',$withdrawInfo);
        return view('Money/withdraw');
    }
    //待提现列表
    public function waitCash()
    {
        $waitCashList = db('withdraw_order w')->where(['w.status'=>0,'w.type'=>2])->join('user u','u.user_id=w.user_id')->field('u.*,w.*')->order('w.create_time','desc')->paginate(10)->each(function ($item,$key){
            $item['actual_amount']      = $item['arrival_money']; 
            return $item;
        });
        $this->assign('waitCashList',$waitCashList);
        return view('Money/waitCash');
    }
    // 审核提现
    public function cash()
    {
        $id     = Request::param('id');
        $type   = Request::param('type');
        $mes    = ['','通过','拒绝'];
        Db::startTrans();
        try {
                $wN     = Db::name('withdraw_order')->where(['withdraw_id'=>$id])->setField('status',$type);
                if ($type == 1)
                {
                    $wInfo    = Db::name('withdraw_order w')->where('withdraw_id',$id)->join('user u','u.user_id=w.user_id')->field('u.user_apliy_name,u.user_apliy_number,u.number_id,w.*')->find();
                    // dump($wInfo);die;
                    $params['out_biz_no']       = $wInfo['order_number'];
                    $params['payee_real_name']  = $wInfo['user_apliy_name'];
                    $params['payee_account']    = $wInfo['user_apliy_number'];
                    $params['amount']           = $wInfo['arrival_money'];
                    $request = new AlipayTransfer();
                    $result = $request->transfer($params);
                    // dump($result);die;
                    $responseNode = str_replace(".", "_", 'alipay.fund.trans.toaccount.transfer') . "_response";
                    $resultCode = $result->$responseNode->code;
                    if(!empty($resultCode)&&$resultCode == 10000){
                        //提现成功以后 更新表状态 
                        //并且记录 流水等等 
                        $wN     = Db::name('withdraw_order')->where(['withdraw_id'=>$id])->setField('status',3);
                    } else {
                        //$result->$responseNode->sub_msg 这个参数 是返回的错误信息 
                      // throw new \Exception($result->$responseNode->sub_msg);
			Db::rollback();return json_error($result->$responseNode->sub_msg);
                    }
                }
                else if ($type == 2) 
                {
                    # 拒绝提现退币
                    $oInfo  = Db::name('withdraw_order')->where('withdraw_id',$id)->find();
                    $uN     = Db::name('user')->where('user_id',$oInfo['user_id'])->setInc('user_gold',$oInfo['money']);
                    $wArr   = [
                        'user_id'       => $oInfo['user_id'],
                        'type'          => 4,
                        'amount'        => $oInfo['money'],
                        'create_time'   => date('Y-m-d H:i:s'),
                        'status'        => 1,
                    ];
                    $lN     = Db::name('wallet_log')->insert($wArr);
                    if (!$lN || !$uN) {Db::rollback();return json_error($mes[$type].'失败');}
                }
                Db::commit();return json_success($mes[$type].'成功');
        } catch (Exception $e) {
            Db::rollback();
            return json_error($mes[$type].'失败');
        }
    }
}
